package com.slmxz.tms.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slmxz.tms.biz.mapper.DispatchMapper;
import com.slmxz.tms.biz.model.dto.DispatchDTO;
import com.slmxz.tms.biz.model.entity.Dispatch;
import com.slmxz.tms.biz.model.entity.Order;
import com.slmxz.tms.biz.model.vo.DispatchVO;
import com.slmxz.tms.biz.service.DispatchService;
import com.slmxz.tms.biz.service.OrderDispatchService;
import com.slmxz.tms.biz.service.OrderService;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.document.model.entity.Contractor;
import com.slmxz.tms.document.model.entity.Driver;
import com.slmxz.tms.document.model.entity.Truck;
import com.slmxz.tms.common.component.NoGeneratorService;
import com.slmxz.tms.common.component.RedisUserService;
import com.slmxz.tms.common.constant.NumberContants;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.document.service.ContractorService;
import com.slmxz.tms.document.service.DriverService;
import com.slmxz.tms.document.service.RouteService;
import com.slmxz.tms.document.service.TruckService;
import com.slmxz.tms.system.model.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import cn.hutool.core.util.StrUtil;

/**
 * 调度计划服务实现类
 *
 * @author sunyao
 *@Date 2025-04-02 21:26
 */
@Service
@RequiredArgsConstructor
public class DispatchServiceImpl extends ServiceImpl<DispatchMapper, Dispatch> implements DispatchService {

    private final OrderService orderService;

    private final OrderDispatchService orderDispatchService;

    private final ContractorService contractorService;

    private final DriverService driverService;

    private final TruckService truckService;

    private final RouteService routeService;
    /**
    * 获取调度计划分页列表
    * @return {@link IPage< DispatchVO >} 调度计划分页列表
    */
    @Override
    public IPage<DispatchVO> selectDispatchPage(IPage<DispatchVO> page,DispatchVO dispatch) {
        return page.setRecords(this.baseMapper.selectDispatchPage(page,dispatch));
    }
    
    /**
     * @param id 调度计划ID
     * @return
     */
    @Override
    public DispatchVO selectDispatchDetail(Long id) {
        Dispatch entity = this.getById(id);
        DispatchVO dispatch = BeanUtil.copyProperties(entity,DispatchVO.class);
        return dispatch;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dispatchOrder(DispatchDTO dispatch) throws Exception {

        return false;
    }
    
    /**
     * 新增调度计划
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDispatch(DispatchVO dispatch) {
        return this.save(dispatch);
    }
    
    /**
     * 更新调度计划
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDispatch(DispatchVO dispatch) {
        return this.updateById(dispatch);
    }
    
    /**
     * 删除调度计划
     *
     * @param ids 调度计划ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDispatchs(String ids) {
        Asserts.fail(StrUtil.isBlank(ids), BizMessageEnum.PARAM_IS_NULL);
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(DispatchVO dispatch) {
        //激活或者注销业务数据
        Asserts.fail(CollectionUtil.isEmpty(dispatch.getIds()),BizMessageEnum.PARAM_IS_NULL);
        List<Dispatch>  dispatchList = new ArrayList<>(dispatch.getIds().size());
        dispatch.getIds().forEach(id -> {
            Dispatch dispatchParam = new Dispatch();
            dispatchParam.setId(id);
            dispatchList.add(dispatchParam);
        });
        this.updateBatchById(dispatchList,10);
        return true;
    }

}
